package mia.beans;

import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import org.hibernate.Session;

import mia.dao.AdvertisementTypeEntityDAO;
import mia.dao.CategoryEntityDAO;
import mia.dao.UserEntityDAO;
import mia.entity.AdvertisementEntity;
import mia.entity.AdvertisementTypeEntity;
import mia.entity.AttachmentEntity;
import mia.entity.CategoryEntity;
import mia.entity.UserEntity;
import mia.util.HibernateManager;

@ManagedBean(name="advertisementEditBean")
@RequestScoped
public class AdvertisementEditBean {

	@ManagedProperty("#{userLoginBean.user}")
	private UserEntity user;
	
	private AdvertisementEntity advertisement = new AdvertisementEntity();
	private AdvertisementEntity editedAdvertisement = new AdvertisementEntity();
	
	private Map<String, AdvertisementTypeEntity> types = null;
	private String selectedType;
	
	private Map<String, CategoryEntity> categories = null;
	private String selectedCategory;
	
	private String attachment1;
	private String attachment2;
	private String attachment3;


	public AdvertisementEntity getAdvertisement() {
		return advertisement;
	}
	public void setAdvertisement(AdvertisementEntity advertisement) {
		this.advertisement = advertisement;
	}
	public AdvertisementEntity getEditAdvertisement() {
		return editedAdvertisement;
	}
	public void setEditAdvertisement(AdvertisementEntity editAdvertisement) {
		this.editedAdvertisement = editAdvertisement;
	}
	public Map<String, AdvertisementTypeEntity> getTypes() {
		if(types == null)
			types = AdvertisementTypeEntityDAO.getAdvertisementTypes();
		return types;
	}
	public void setTypes(Map<String, AdvertisementTypeEntity> types) {
		this.types = types;
	}
	public Map<String, CategoryEntity> getCategories() {
		if(categories == null)
			categories = CategoryEntityDAO.getCategories();
		return categories;
	}
	public void setCategories(Map<String, CategoryEntity> categories) {
		this.categories = categories;
	}
	public String getSelectedType() {
		return selectedType;
	}
	public void setSelectedType(String selectedType) {
		this.selectedType = selectedType;
	}
	public String getSelectedCategory() {
		return selectedCategory;
	}
	public void setSelectedCategory(String selectedCategory) {
		this.selectedCategory = selectedCategory;
	}
	public String getAttachment1() {
		return attachment1;
	}
	public void setAttachment1(String attachment1) {
		this.attachment1 = attachment1;
	}
	public String getAttachment2() {
		return attachment2;
	}
	public void setAttachment2(String attachment2) {
		this.attachment2 = attachment2;
	}
	public String getAttachment3() {
		return attachment3;
	}
	public void setAttachment3(String attachment3) {
		this.attachment3 = attachment3;
	}
	public UserEntity getUser() {
		return user;
	}
	public void setUser(UserEntity user) {
		this.user = user;
	}	

	public void tryEdit()
	{
		FacesContext context = FacesContext.getCurrentInstance();
		AttachmentEntity att1 = new AttachmentEntity();
		att1.setPath(attachment1);
		att1.setAdvertisement(advertisement);
		
		AttachmentEntity att2 = new AttachmentEntity();
		att2.setPath(attachment2);
		att2.setAdvertisement(advertisement);
		
		AttachmentEntity att3 = new AttachmentEntity();
		att3.setPath(attachment3);
		att3.setAdvertisement(advertisement);

		Session session = HibernateManager.getSession();
		session.beginTransaction();
		UserEntity u = UserEntityDAO.getUserByLogin(session, user.getLogin());
		
		advertisement.setUser(u);
		advertisement.getAttachments().add(att1);
		advertisement.getAttachments().add(att2);
		advertisement.getAttachments().add(att3);
		System.out.println("Kategoria"+getCategories().size());
		advertisement.setCategory(getCategories().get(selectedCategory));
		advertisement.setType(getTypes().get(selectedType));
		
		try
		{
			session.save(advertisement);
			for(AttachmentEntity at : advertisement.getAttachments())
				session.save(at);
			session.getTransaction().commit();
			context.addMessage("adding",new FacesMessage(FacesMessage.SEVERITY_INFO, "Dodano ogłoszenie",""));
			
			
		}
		catch (Exception ex)
		{
			session.getTransaction().rollback();
			context.addMessage("change",new FacesMessage(FacesMessage.SEVERITY_ERROR, "Wystąpił błąd, ogłoszenie nie zostało dodane!",""));
			
		}
		finally
		{
			session.close();
		}
	}
}
